<?php
/**
 * Created by PhpStorm. 百度翻译
 * User: xpz
 * Date: 2022/3/21
 * Time: 16:08
 */

namespace app\api\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use fast\BaiduTransapi;

class BaiduTrans extends Command
{

    protected function configure()
    {
        $this->setName('baidutrans')              		//定义命令的名字
        ->setDescription('baidutrans')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('url')                   //文章url
        ->addArgument('id');                    //文章id
    }

    //php think baidutrans chengyu(publish)
    protected function execute(Input $input, Output $output)
    {

        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义

        $method = $input->getArgument('method');
        if($method == 'chengyu'){
            $this->chengyu();
        } else if($method == 'pinyin'){
            $this->pinyin();
        }
    }

    private function chengyu(){
        $chengyuModel = Model('app\common\model\Chengyu');
        $chengyuRs = $chengyuModel->where(['liju'=>['neq','']])->limit(5000)->select();
//        print_r(count($chengyuRs));exit;
        if(!empty($chengyuRs)){
            foreach($chengyuRs as $k=>$v){
//                print_r($v);exit;
//                if(!empty($v['content'])){
//                    $preg = '/<p>(.*?)<\/p>/';
//                    preg_match_all($preg, $v['content'], $match);
//                    $jieshi = str_replace('<span>解释：</span>','',$match[1][3]);
//                } else {
//                    $jieshi = $v['title'];
//                }
//
//
//                $en = BaiduTransapi::translate($jieshi,'zh','en');
////                print_r($en);exit;
//                $enPinyin = $en['trans_result'][0]['dst'];
//                echo "【".$v['title']."】英文解释为：".$enPinyin."\r\n";
//                $chengyuModel->where(['id'=>$v['id']])->update(['en_jieshi'=>$enPinyin]);

                //例句
                if(!empty($v['liju'])){
                    $liju = str_replace(['～','<p>','</p>','1.'],[$v['title'],'','',''],$v['liju']);
                    $en = BaiduTransapi::translate($liju,'zh','en');
                    $enPinyin = $en['trans_result'][0]['dst'];
                    echo "【".$v['title']."】例子中文为：".$liju."\r\n";
                    echo "【".$v['title']."】例子英文为：".$enPinyin."\r\n";
                    $chengyuModel->where(['id'=>$v['id']])->update(['en_liju'=>$enPinyin]);
                } else {
                    echo "【".$v['title']."】例子为空\r\n";
                }

            }
        }
    }
//php think baidutrans pinyin
    private function pinyin(){
//        $chengyuModel = Model('app\common\model\Chengyu');
//        $chengyuRs = $chengyuModel->limit(40000,10000)->order("id descs")->select();
//        if(!empty($chengyuRs)){
//            foreach($chengyuRs as $k=>$v){
//              if(!empty($v['pinyin_all'])){
//                  echo "pinyin_all有数据";
//                $newPinyin = str_replace([$v['title'],'[',']'],['','',''],$v['pinyin_all']);
//              } else {
//                  $preg = '/<p>(.*?)<\/p>/';
//                  preg_match_all($preg, $v['content'], $match);
//                  $newPinyin  = str_replace('<span>拼音：</span>','',$match[1][1]);
//              }
//                echo "【".$v['title']."】拼音为：".$newPinyin."\r\n";
//              if(!empty($newPinyin)){
//                  $chengyuModel->where(['id'=>$v['id']])->update(['pinyin_all'=>$newPinyin]);
//              }
//
//            }
//        }
    }

}